package gonz.pages.swim;

import gonz.common.Waiter;
import gonz.data.swim.ItemType;
import gonz.data.swim.OnePiecesSwim;
import gonz.data.swim.Swim;
import gonz.data.swim.TwoPiecesSwim;
import gonz.pages.AbstractPage;
import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.util.ArrayList;
import java.util.List;

public class SwimIndexPage extends AbstractPage {
    public static final String BIKINIS_CATEGORY_ID = "swimwearbikinis";
    public static final String TANKINIS_CATEGORY_ID = "swimweartankinis";
    public static final String ONE_PIECES_ID = "swimwearone-piece";
    public static final String COVER_UP_ID = "swimwearcover-ups";
    public static final String FIRST_SWIM_XPATH = "//ul[@id='collection']/li/ul/li/a";
    //ul[@id='collection']/li/ul/li/a/span/img
    public static final String VIEW_ALL_XPATH = "//ul[@id='top-pagination']/li/a";
    public static final String COLLECTION_XPATH = "//ul[@id='collection']/li/ul/li/a";
    public static final Logger log = Logger.getLogger(SwimIndexPage.class);
    public static final String FOOTER_ID = "footer";

    public SwimIndexPage(WebDriver webDriver) {
        super(webDriver);
        Waiter.waitElement(webDriver, By.id(FOOTER_ID));
    }

    public SwimIndexPage(WebDriver webDriver, String link) {
        super(webDriver);
        webDriver.get(link);
        Waiter.waitElement(webDriver, By.id(FOOTER_ID));
    }

    public Swim openFirstSwim(String imgFolder, ItemType itemType) {
        Waiter.waitElement(webDriver, By.xpath(FIRST_SWIM_XPATH));
        webDriver.findElement(By.xpath(FIRST_SWIM_XPATH)).click();
        switch (itemType) {
            case TWO_PEACE:
                return new TwoPiecesSwim(webDriver, imgFolder);
            case ONE_PEACE:
                return new OnePiecesSwim(webDriver, imgFolder);
            default:
                return new OnePiecesSwim(webDriver, imgFolder);
        }

    }

    public void clickViewAll() {
        log.info("Clicking View All.");
        List<WebElement> viewAllElements = webDriver.findElements(By.xpath(VIEW_ALL_XPATH));
        if (viewAllElements.isEmpty()) {
            log.info("View all button is not found.");
        } else {
            String hrefAttr = viewAllElements.get(0).getAttribute("href");
            webDriver.get(hrefAttr);
            Waiter.waitElement(webDriver, By.id(FOOTER_ID));
            log.info("View all button has been clicked.");
        }

    }

    public List<String> getItemLinks() {
        List<WebElement> items = webDriver.findElements(By.xpath(COLLECTION_XPATH));
        List<String> result = new ArrayList<String>();
        log.info(items.size() + " items has been found.");
        for (WebElement item : items) {
            String hrefAttr = item.getAttribute("href");
            result.add(hrefAttr);
        }
        return result;
    }
}
